package dcw.excel;

import dcw.core.common.Tuple;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2021/12/1016:07
 */
public class CreateUtil {

    public static String appendPrivate(List<Tuple> tuples){
        StringBuffer sb = new StringBuffer();
        for(Tuple tuple: tuples){
            sb.append("/**\n");
            sb.append(" *"+tuple.get(2)+"\n");
            sb.append(" */\n");
            sb.append(" private "+tuple.get(0)+" "+tuple.get(1)+";\n\n");
        }
        return sb.toString();
    }

    public static String getSet(List<Tuple> tuples){
        StringBuffer sb = new StringBuffer();
        for(Tuple tuple: tuples){
            String attribute = tuple.get(1);
            String c = String.valueOf(attribute.charAt(0));
            c = c.toUpperCase();
            attribute = c + attribute.substring(1);
            sb.append("/**\n");
            sb.append(" * 设置"+tuple.get(2)+"\n");
            sb.append(" * @param "+tuple.get(1)+" "+tuple.get(2)+"\n");
            sb.append(" */\n");
            sb.append(" public void set"+attribute+"("+tuple.get(0)+" "+tuple.get(1)+"){\n");
            sb.append("     this."+tuple.get(1)+" = "+tuple.get(1)+";\n");
            sb.append(" }\n\n");
            sb.append("/**\n");
            sb.append(" * 获得"+tuple.get(2)+"\n");
            sb.append(" * @return "+tuple.get(1)+" "+tuple.get(2)+"\n");
            sb.append(" */\n");
            sb.append(" public "+tuple.get(0)+" get"+attribute+"(){\n");
            sb.append("     return "+tuple.get(1)+";\n");
            sb.append(" }\n\n");
        }

        return sb.toString();
    }


    public static void createJava(String javaName,List<Tuple> tuples) {

        StringBuffer sb = new StringBuffer();

        sb.append("public class "+ javaName +"{\n\n");
        sb.append(appendPrivate(tuples));
        sb.append(getSet(tuples));
        sb.append("}");

        File file1 = new File("d:/"+javaName+".java");
        FileOutputStream fop = null;
        try {
            fop = new FileOutputStream(file1);
            if (!file1.exists()) {
                file1.createNewFile();
            }
            byte[] contentInBytes = sb.toString().getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(javaName +"　Done");
    }

    public static void main(String[] args) throws IOException {
        List<Tuple> tupleList = new ArrayList<>();
        Tuple tuple = new Tuple("int[]","event","测试数据");
        Tuple tuple1 = new Tuple("int","age","年龄");
        tupleList.add(tuple);
        tupleList.add(tuple1);
        createJava("Test",tupleList);
    }
}
